<?php
include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/ml_catelog.php');
include(SERVER_ROOT_PATH.'/include/config/ml_guang_config.php');

class aj_scrolltag extends ml_controller
{
    private $order;
    private $tgid;
    private $block;
    private $check_rid=0;
    private $color;
    private $minPrice;
    private $maxPrice;
    private $filter;
    
    public function initParam()
    {
        $this->order = $this->input('order','G') == 'new' ? 'new' : 'hot';
        $this->tgid = $this->input('tag','G');

        $page = (int)$this->input('page' , 'G' , 1);
        $block = (int)$this->input('segment' , 'G'  , 1);
        $this->tj_rid=$this->input('tj_rid','g');

        $block = $block<1 ? 1 : $block;
        $block = $block>ML_GUANG_BLOCK ? ML_GUANG_BLOCK : $block;
        $page = $page < 1 ? 1 : $page;
        $page = $page > ML_GUANG_TOTAL_PAGE ? 1 : $page;
        $this->block = ($page-1)*ML_GUANG_BLOCK + $block-1;
        $this->color = $this->input('color');
        $this->maxPrice = $this->input('maxPrice');
        $this->minPrice = $this->input('minPrice');
        $this->filter = $this->input('filter');
        define('ML_SHOW_GOODS_FRM', 'pb'.$this->block);
    }
    public function checkParam()
    {
        $this->check_referer();
        //        $aLmTag = ml_factory::load_standard_conf('lmTag');
        //        if(!isset($aLmTag[$this->tgid]))
        //            $this->api_output(ML_RCODE_HACK);

        if(!empty($this->tj_rid)){
            if(ml_tool_resid::is_resid($this->tj_rid)){
                $this->check_rid=1;
            }
        }

    }
    public function main()
    {
        $page_type = 'tag_'.$this->order;

        //行为LOG start
        $aTarget = array(
            'tag' => $this->tgid,
            'order' => $this->order,
            'block' => $this->block,
        );
        if(!empty($this->filter)){
            $aTarget['filter'] = $this->filter;
        }
        if(!empty($this->color))
            $aTarget['color'] = $this->color;
        if((!empty($this->minPrice)) || (!empty($this->maxPrice))){
            $aTarget['price'] = trim($this->minPrice.'_'.$this->maxPrice, '_');
        }
        ml_tool_actionlog::view_guang($this->__visitor['uid'] , $aTarget);
        //行为LOG end

        global $ML_CATELOG_REV;
        $option = array('sort'=>$this->order,'tag'=>array($this->filter,$this->tgid),'color'=>$this->color,'maxPrice'=>$this->maxPrice,'minPrice'=>$this->minPrice,'block'=>$this->block);        
        $oGuang = new ml_biz_guang();
        $oGuang->check($option);
        $content = $oGuang->main();
        $oPin = new ml_biz_pin_content();
        $data['items'] = $oPin->output($this->__visitor['uid'], $content['rows'], false,true);
        
        if($this->check_rid==1){
            $html=$this->getPinData();
            if($html != false){
                array_unshift($data['items'],$html);
                array_pop($data['items']);
            }
        }

        foreach ($data['items'] as $value) {
            $str.=$value;
        }
        $data['items']=$str;
        $data['lastSegment'] = $this->block == ceil($content['total']/ML_GUANG_PAGESIZE)-1 ? true : false;


        $this->api_output(ML_RCODE_SUCC , $data);
    }

    public function getPinData(){
        $oContent=new ml_model_dbContent();
        $rs=$oContent->get_by_rid($this->tj_rid);
        if($rs){
            $conData=$oContent->get_data();
            if(isset($conData['uid'])){
                $oPinCon=new ml_biz_pin_content();
                $data=$oPinCon->output($conData['uid'],array($conData),false);
                return $data[0];
            }
        }
        return false;
    }

}
new aj_scrolltag();